package org.team4.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.constants.SQLConstant;
import org.team4.constants.StringConstant;
import org.team4.dao.DAOFactory;
import org.team4.dao.meta.RoleUsersDAO;
import org.team4.domain.Role;
import org.team4.domain.RoleUsers;
import org.team4.domain.Users;
import org.team4.exception.CRUDException;
import org.team4.service.meta.RoleCacheService;
import org.team4.util.Validate;

import org.team4.service.IService;

@Service
public class RoleUsersService implements IService {

    private RoleUsersDAO roleUsersDAO;
    @Autowired
    private RoleCacheService roleChangedService;
    
    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        roleUsersDAO = (RoleUsersDAO) DAOFactory.getDAO(sessionFactory, RoleUsersDAO.class);
    }

    /**
     * 根据角色ID查找用户权限
     * 
     * @param roleId
     * @return
     * @throws CRUDException
     */
    public List<RoleUsers> findRoleUsersByRole(String roleId) throws CRUDException {
        return roleUsersDAO.find(SQLConstant.ROLEUSERS_BY_ROLE_HQL, roleId);
    }

    /**
     * 根据用户ID和角色ID查找用户角色的关联记录
     * 
     * @param usersId
     * @param roleId
     * @return
     * @throws CRUDException
     */
    public RoleUsers findUsersRoleByUsersAndRole(String usersId, String roleId) throws CRUDException {
        List<RoleUsers> ru = roleUsersDAO.find(SQLConstant.ROLEUSERS_BY_USERS_ROLE_HQL, usersId, roleId);
        if (Validate.collectionNotNull(ru))
            return ru.get(0);
        else
            return null;
    }

    /**
     * 保存用户角色
     * 
     * @param idList
     * @param usersId
     * @param state
     * @throws CRUDException
     */
    public void saveUsersRole(List<String> idList, String usersId, String state) throws CRUDException {
        if (Validate.collectionNotNull(idList))
            // 保存
            if (StringConstant.TRUE.equals(state)) {
                for (String roleId : idList) {
                    // 当用户角色未添加时,添加用户角色
                    if (!Validate.idNotNull(findUsersRoleByUsersAndRole(usersId, roleId))) {
                        Role r = new Role();
                        r.setId(roleId);

                        Users u = new Users();
                        u.setId(usersId);

                        RoleUsers ru = new RoleUsers();
                        ru.setRole(r);
                        ru.setUsers(u);

                        roleUsersDAO.save(ru);

                    }
                }
                //刷新用户角色
                roleChangedService.flushRoleCache(usersId);
            }
            // 删除
            else {
                for (String roleId : idList) {
                    // 当用户角色已添加时,删除用户角色
                    RoleUsers ru = findUsersRoleByUsersAndRole(usersId, roleId);
                    if (Validate.idNotNull(ru)) {
                        roleUsersDAO.delete(ru);
                    }
                }
                // 刷新用户角色
                roleChangedService.flushRoleCache(usersId);
            }
    }

    /**
     * 角色关联用户
     * 
     * @param idList
     * @param roleId
     * @param state
     * @throws CRUDException
     */
    public void saveRoleUsers(List<String> idList, String roleId, String state) throws CRUDException {
        if (Validate.collectionNotNull(idList)) {
            // 添加用户
            if (StringConstant.TRUE.equals(state)) {
                for (String usersId : idList) {
                    // 当用户角色未添加时,添加用户角色
                    if (!Validate.idNotNull(findUsersRoleByUsersAndRole(usersId, roleId))) {
                        Role r = new Role();
                        r.setId(roleId);

                        Users u = new Users();
                        u.setId(usersId);

                        RoleUsers ru = new RoleUsers();
                        ru.setRole(r);
                        ru.setUsers(u);

                        roleUsersDAO.save(ru);

                        // 将用户ID放入角色变更集合中
                        roleChangedService.flushRoleCache(usersId);
                    }
                }
            }
            // 删除用户
            else {
                for (String usersId : idList) {
                    // 当用户角色已添加时,删除用户角色
                    RoleUsers ru = findUsersRoleByUsersAndRole(usersId, roleId);
                    if (Validate.idNotNull(ru)) {
                        roleUsersDAO.delete(ru);

                        // 将用户ID放入角色变更集合中
                        roleChangedService.flushRoleCache(usersId);
                    }
                }
            }
        }
    }
    
    /**
     * 根据用户ID查找用户权限
     * 
     * @param usersId
     * @return
     * @throws CRUDException
     */
    public List<RoleUsers> findUsersRoleByUsers(String usersId) throws CRUDException {
        return roleUsersDAO.findUsersRoleByUsers(usersId);
    }
    
}
